let layer = layui.layer;
let form = layui.form
// 获取列表
function get() {
    $.ajax({
        url: '/my/article/cates',
        success: function (res) {
            console.log(res);
            let str = ''
            res.data.forEach(function (item) {
                str += `
            <tr>
                    <td>${item.name}</td>
                    <td>${item.alias}</td>
                    <td>
						<button myid="${item.Id}" data-name="${item.name}" data-alias="${item.alias}" type="button" class="layui-btn layui-btn-xs edit">编辑</button>

                      	<button myid="${item.Id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger delete">删除</button>
                    </td>
                  </tr>
            `
            })
            $('tbody').html(str)
        }
    })
}
get();

// 删除内容

$('tbody').on('click', '.delete', function () {

    layer.confirm("确认要删除吗？", (index) => {
        let id = $(this).attr('myid')
        $.ajax({
            url: '/my/article/deletecate/' + id,
            success: function (res) {
                layer.msg(res.message);
                if (res.status == 0) {
                    get();
                }
            }
        })
    })
})


// 添加内容
let box = `
  <form class="layui-form add-form" action="" style="margin: 30px; margin-left: 0px;" id="add_form">
    <div class="layui-form-item">
      <label class="layui-form-label">类别名称</label>
      <div class="layui-input-block">
        <input type="text" name="name" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">类别别名</label>
      <div class="layui-input-block">
        <input type="text" name="alias" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>`
$('.btn').on('click', function () {

    let index = layer.open({
        type: 1,
        content: box,
        title: '添加类别',
        area: ['500px', '250px'],
    });
    $('form').on('submit', function (e) {
        e.preventDefault();
        let data = $(this).serialize();
        $.ajax({
            url: '/my/article/addcates',
            type: 'POST',
            data: data,
            success: function (res) {
                layer.msg(res.message);
                if (res.status == 0) {
                    get();
                    layer.close(index);
                }
            }
        })
    })
})